package gui;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;

import org.netbeans.api.visual.anchor.AnchorShape;
import org.netbeans.api.visual.anchor.PointShape;
import org.netbeans.api.visual.graph.GraphScene;
import org.netbeans.api.visual.router.Router;
import org.netbeans.api.visual.router.RouterFactory;
import org.netbeans.api.visual.widget.ConnectionWidget;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Widget;
import org.netbeans.api.visual.widget.general.IconNodeWidget;
import org.openide.util.ImageUtilities;

import parser.Block;

public class GraphicalBlockRepresentation extends GraphScene<Block,Block>
{

	private static final Image BLOCK_ICON = ImageUtilities.loadImage("Images/Block.png"); 
	private LayerWidget blkLayerWidget;
	private LayerWidget connections;
	private Router router = RouterFactory.createFreeRouter();
	private int imageWidth;
	private int imageHeight;
	
	public GraphicalBlockRepresentation(){
		blkLayerWidget = new LayerWidget(this);
		connections = new LayerWidget(this);
		addChild(blkLayerWidget);
		addChild(connections);
		initPage();
	}
	
	
	private void initPage() {
        Image sourceImage = ImageUtilities.loadImage("Images/backPoints.png");
        int width = sourceImage.getWidth(null);
        int height = sourceImage.getHeight(null);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();
        graphics.drawImage(sourceImage, 0, 0, null);
        graphics.dispose();
        TexturePaint background = new TexturePaint(image, new Rectangle(0, 0, width, height));
        setBackground(background);
        repaint();
        revalidate(false);
        validate();	
	}


	@Override
	protected Widget attachEdgeWidget(Block arg0) {
		ConnectionWidget edge = new ConnectionWidget(this);
		edge.setRouter(router);
	 //   conexion.setToolTipText(String.valueOf(arco.getCosto()));
		edge.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
		edge.setControlPointShape(PointShape.SQUARE_FILLED_BIG);
		edge.setEndPointShape(PointShape.SQUARE_FILLED_BIG);
	    connections.addChild(edge);
	    edge.getActions().addAction(createSelectAction());
	    
	    return edge;
	}

	@Override
	protected Widget attachNodeWidget(Block nod) {
		//TODO - CHECK THE MOVERS
		IconNodeWidget node = new IconNodeWidget(this);
		node.setToolTipText(nod.getBlockName());
		node.setLabel(nod.getBlockName());
		int width = imageWidth;
	    int height = imageHeight;
	    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();
        graphics.drawImage(BLOCK_ICON, 0, 0, null);
		node.setImage(image);
		blkLayerWidget.addChild(node);
		return node;
	}

	@Override
	protected void attachEdgeSourceAnchor(Block arg0, Block arg1, Block arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void attachEdgeTargetAnchor(Block arg0, Block arg1, Block arg2) {
		// TODO Auto-generated method stub
		
	}


	public int getImageWidth() {
		return imageWidth;
	}


	public void setImageWidth(int imageWidth) {
		this.imageWidth = imageWidth;
	}


	public int getImageHeight() {
		return imageHeight;
	}


	public void setImageHeight(int imageHeight) {
		this.imageHeight = imageHeight;
	}
	

}
